home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE14 / TREE / FILELIST.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-06-27  |  3.3 KB  |  105 lines

  1. unit Filelist;
  2.  
  3. interface
  4.  
  5. uses Classes,SysUtils,Controls,Forms;
  6.  
  7. type
  8.   TFileList = Class(TObject)
  9.     {Only code in the current unit can access private data fields & methods}
  10.     private
  11.       File_Mask: string;
  12.       Sub_Directories: boolean;
  13.       procedure Search_Directory ( const Directory_FileMask: string );
  14.       procedure Found_File_Dir ( const SearchRec: TSearchRec; FileDir_List: TStringList);
  15.     {All items in the public section can be referenced from any other unit}
  16.     public
  17.       Files_Found: TStringList;
  18.       constructor Create (const Directory_FileMask: string; const Sub_Dirs: boolean);
  19.       destructor Destroy; override;
  20.   end; { TFileList }
  21.  
  22. implementation
  23.  
  24. {The next line will make all run-time I/O errors into exceptions.}
  25. {$I+}
  26.  
  27. constructor TFileList.Create (const Directory_FileMask: string; const Sub_Dirs: boolean);
  28. var
  29.    Old_Screen_Cursor: TCursor;
  30. begin
  31.   {Execute the ancestor constructor}
  32.   inherited Create;
  33.   Old_Screen_Cursor:= Screen.Cursor;
  34.   Screen.Cursor:=     crHourGlass;
  35.   Application.ProcessMessages;
  36.   try
  37.      Files_Found:= TStringList.Create;
  38.      Files_Found.Sorted:=     False;
  39.      File_Mask:= ExtractFileName( Directory_FileMask );
  40.      Sub_Directories:= Sub_Dirs;
  41.      Search_Directory ( Directory_FileMask );
  42.   finally
  43.          Screen.Cursor:= Old_Screen_Cursor;
  44.   end;
  45. end; {. Create}
  46.  
  47. destructor TFileList.Destroy;
  48. begin
  49.      Files_Found.Free;
  50.      {Execute the ancestor constructor}
  51.      inherited Destroy;
  52. end; {. Destroy}
  53.  
  54. procedure TFileList. Search_Directory ( const Directory_FileMask: string );
  55. var
  56.    SearchRec:   TSearchRec;
  57.    Directory:   string;
  58.    FilesDirsFound:  TStringList;
  59.    c1:          integer;
  60. begin
  61.   Directory:= ExtractFilePath (Directory_FileMask);
  62.   FilesDirsFound:= TStringList.Create;
  63.   FilesDirsFound.Sorted:= True;
  64.   FilesDirsFound.Duplicates:= dupError;
  65.   try
  66.   {Build a list of files but not directories in current directory}
  67.   if FindFirst (Directory_FileMask, faAnyFile - faDirectory, SearchRec) = 0 then
  68.     begin
  69.       Found_File_Dir ( SearchRec,FilesDirsFound);
  70.       while ( FindNext (SearchRec) = 0 ) do
  71.          Found_File_Dir ( SearchRec,FilesDirsFound);
  72.     end;
  73.   FindClose (SearchRec);
  74.   {Now copy list of sorted files found on to public stringlist of unsorted files}
  75.   for c1:= 0 to FilesDirsFound.Count-1 do
  76.       Files_Found.Add( Directory + FilesDirsFound[c1] );
  77.   {Now start checking the sub-directories}
  78.   if Sub_Directories then
  79.      begin
  80.        FilesDirsFound.Clear;
  81.        if FindFirst (Directory + '*.*', faDirectory, SearchRec) = 0 then
  82.        begin
  83.             Found_File_Dir ( SearchRec,FilesDirsFound);
  84.             while ( FindNext (SearchRec) = 0 ) do
  85.                   Found_File_Dir ( SearchRec,FilesDirsFound);
  86.        end;
  87.        FindClose (SearchRec);
  88.        for c1:= 0 to FilesDirsFound.Count-1 do
  89.            Search_Directory ( Directory + FilesDirsFound[c1] + '\' + File_Mask);
  90.      end; { if Sub_Directories  }
  91.   finally
  92.          FilesDirsFound.Free;
  93.   end;
  94. end; {. Search_Directory}
  95.  
  96.  
  97. procedure TFileList. Found_File_Dir ( const SearchRec: TSearchRec; FileDir_List: TStringList);
  98. begin
  99.   if ( (SearchRec.Name <> '.') and (SearchRec.Name <> '..') ) then
  100.          FileDir_List.Add (SearchRec.Name)
  101. end; {. Found_File }
  102.  
  103.  
  104. end.
  105.